package com.qf.index;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Test {

    public static void main(String[] args) throws SQLException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date()) + " 程序开始了~~~");
        String newDataName = "scholar_25_02.";
        String summaryTable = newDataName + "unified_search_summary_0308";
        String bibliometricTable = newDataName + "bibliometric_pm_0308_test";

        Map<String, String> cnEnCountryMap = cList.getCnEnCountryMap();

//        Entity entry = Db.use().queryOne("select `info` from " + bibliometricTable + " where `pmid` = " + 34556713 + " limit 1");
//        En.TableData tableData = JSONObject.parseObject(entry.getStr("info"), En.TableData.class);


//        Set<Integer> aidSet = new HashSet<>();
//        int sum = wosToText(aidSet, filePath, summaryTable, false);
//        System.out.println("成功下载：" + sum);

        Set<Integer> pmIdSet = new HashSet<>();
        String s = "1222193\n" +
                "17748315\n" +
                "8318765\n" +
                "19017848\n" +
                "13947986\n" +
                "2918817\n" +
                "13993963\n" +
                "5327511\n" +
                "1233141\n" +
                "17784433\n" +
                "26045262\n" +
                "7758065\n" +
                "12730730\n" +
                "17793317\n" +
                "6524974\n" +
                "5395494\n" +
                "13933929\n" +
                "5908195\n" +
                "7123625\n" +
                "17761273\n" +
                "18356992\n" +
                "8337677\n" +
                "13992914\n" +
                "10737876\n" +
                "7700696\n" +
                "5969997\n" +
                "5379639\n" +
                "18339919\n" +
                "15808924\n" +
                "2953184\n" +
                "7124725\n" +
                "6541204\n" +
                "17756006\n" +
                "1285843\n" +
                "10770905\n" +
                "5995895\n" +
                "7709538\n" +
                "19097667\n" +
                "8343156\n" +
                "2963839\n" +
                "6555500\n" +
                "13982262\n" +
                "5993969\n" +
                "13969785\n" +
                "7754529\n" +
                "5388719\n" +
                "5967501\n" +
                "8369150\n" +
                "13959624\n" +
                "5997465\n" +
                "1230671\n" +
                "7115101\n" +
                "12732703\n" +
                "10735038\n" +
                "26022011\n" +
                "17705796\n" +
                "5964020\n" +
                "5302803\n" +
                "15834953\n" +
                "10789241\n" +
                "7130543\n" +
                "13955504\n" +
                "1216701\n" +
                "6589256\n" +
                "7121280\n" +
                "7720196\n" +
                "19064639\n" +
                "18342957\n" +
                "6598145\n" +
                "5979668\n" +
                "7726204";
        String[] split = s.split("\n");
        for (String pmId : split) {
            if (NumberUtil.isInteger(pmId)) {
                pmIdSet.add(Integer.parseInt(pmId));
            }
        }
        String filePath = "C:\\临时文件\\2024_12\\wos10.txt";
        // 依据pmId从bibliometricTable表中导出纯文本数据，number为返回的导出的条数。
        int number = Bibliometric.pmToText(pmIdSet, filePath, bibliometricTable);
        System.out.println("number: " + number);

    }


    public static Map<String, String> getKeyWordAndStandardMap(String keyWordSumTable, Map<String, String> keyWordAndMeshMap, Map<String, String> md5Map, Map<String, String> reKwMap, String autoId, String field) throws SQLException {
        int onePageNum = 1000;
        Map<String, String> keyWordAndStandardMap = new ConcurrentHashMap<>();
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + keyWordSumTable);
        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + keyWordSumTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + keyWordSumTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    int isCn = Utils.getIntField(entry, "is_cn");
                    int sum = Utils.getIntField(entry, "sum");
                    String keyWord = Utils.getStrField(entry, "key_word");
                    if (isCn == 0 && keyWord.contains("\"]")) {
                        List<String> keyWordList = JSONObject.parseArray(keyWord, String.class);
                        if (sum > 50 || (keyWordList.size() == 2 && sum > 10) || (keyWordList.size() == 3 && sum > 1)) {
                            String kw = ReadKw.reRemaining(Utils.reKw(reKwMap, keyWordList.get(0)));
                            String standard = keyWordAndMeshMap.getOrDefault(keyWordList.get(0), kw);
                            En.InfoAndStandard infoAndStandard = ReadKw.getInfoAndStandard(kw, standard, md5Map, reKwMap);
                            keyWordAndStandardMap.put(infoAndStandard.getInfo(), infoAndStandard.getStandard());
                        }
                    } else if (isCn == 1 && sum > 30 && keyWord.length() > 1) {
                        En.InfoAndStandard infoAndStandard = ReadKw.getInfoAndStandard(keyWord, keyWord, md5Map, reKwMap);
                        keyWordAndStandardMap.put(infoAndStandard.getInfo(), infoAndStandard.getStandard());
                    }
                }
            }
        }
        return keyWordAndStandardMap;
    }

    /**
     *         int pmId = tableData.getPmId();
     *         String di = tableData.getDi();
     *         String mesh = tableData.getMesh();
     *         String abText = tableData.getAbText();
     *         String keyword = tableData.getKeyword();
     *         String title = tableData.getTitle();
     *         String journal = tableData.getJournal();
     *         int year = tableData.getYear();
     *         int isCns = tableData.getIsCns();
     *         Set<String> ptSet = tableData.getPtSet();
     *         int isChina = tableData.getIsChina();
     *         String nlmId = tableData.getNlmId();
     *         List<String> countryList = tableData.getCountryList();
     *         List<En.SortAuthAndUnit> sortAuthList = tableData.getSortAuthList();
     *         List<En.CategoryAndInfo> drugGenePathwayDiseaseList = tableData.getDrugGenePathwayDiseaseList();
     *         String info = tableData.getInfo();
     *         String ncbiMesh = tableData.getNcbiMesh();
     *         List<String> ncbiSubject = tableData.getNcbiSubject();
     *         double jcrIf = tableData.getJcrIf();
     *         double citeScore = tableData.getCiteScore();
     *         int zkyDaLei = tableData.getZkyDaLei();
     *         int jcrQuartile = tableData.getJcrQuartile();
     *         String webSite = tableData.getWebSite();
     *         String lan = tableData.getLan();
     *         String wc = tableData.getWc();
     *         Set<List<String>> extractTitle = tableData.getExtractTitle();
     *         Set<List<String>> extractAbstract = tableData.getExtractAbstract();
     *         Set<List<String>> keyWordSet = tableData.getKeyWordSet();
     *         List<String> fundList = tableData.getFundList();
     *         List<En.AuthListAndInfo> rpList = tableData.getRpList();
     *         List<En.AuthListAndInfo> c1List = tableData.getC1List();
     *         List<En.TiAuJ9> citationList = tableData.getCitationList();
     *         int total = tableData.getTotal();
     *         List<String> referenceList = tableData.getReferenceList();
     *         List<String> kwPlusList = tableData.getKwPlusList();
     */

//        System.out.println("----pmId:|" + tableData.getPmId() + "|");
//        System.out.println("----di:|" + tableData.getDi() + "|");
//        System.out.println("----mesh:|" + tableData.getMesh() + "|");
//        System.out.println("----abText:|" + tableData.getAbText() + "|");
//        System.out.println("----keyword:|" + tableData.getKeyword() + "|");
//        System.out.println("----title:|" + tableData.getTitle() + "|");
//        System.out.println("----journal:|" + tableData.getJournal() + "|");
//        System.out.println("----year:|" + tableData.getYear() + "|");
//        System.out.println("----isCns:|" + tableData.getIsCns() + "|");
//        System.out.println("----ptSet:|" + tableData.getPtSet() + "|");
//        System.out.println("----isChina:|" + tableData.getIsChina() + "|");
//        System.out.println("----nlmId:|" + tableData.getNlmId() + "|");
//        System.out.println("----countryList:|" + tableData.getCountryList() + "|");
//        System.out.println("----sortAuthList:|" + tableData.getSortAuthList() + "|");
//        System.out.println("----drugGenePathwayDiseaseList:|" + tableData.getDrugGenePathwayDiseaseList() + "|");
//        System.out.println("----info:|" + tableData.getInfo() + "|");
//        System.out.println("----ncbiMesh:|" + tableData.getNcbiMesh() + "|");
//        System.out.println("----ncbiSubject:|" + tableData.getNcbiSubject() + "|");
//        System.out.println("----jcrIf:|" + tableData.getJcrIf() + "|");
//        System.out.println("----citeScore:|" + tableData.getCiteScore() + "|");
//        System.out.println("----zkyDaLei:|" + tableData.getZkyDaLei() + "|");
//        System.out.println("----jcrQuartile:|" + tableData.getJcrQuartile() + "|");
//        System.out.println("----webSite:|" + tableData.getWebSite() + "|");
//        System.out.println("----lan:|" + tableData.getLan() + "|");
//        System.out.println("----wc:|" + tableData.getWc() + "|");
//        System.out.println("----extractTitle:|" + tableData.getExtractTitle() + "|");
//        System.out.println("----extractAbstract:|" + tableData.getExtractAbstract() + "|");
//        System.out.println("----keyWordSet:|" + tableData.getKeyWordSet() + "|");
//        System.out.println("----fundList:|" + tableData.getFundList() + "|");
//        System.out.println("----rpList:|" + tableData.getRpList() + "|");
//        System.out.println("----c1List:|" + tableData.getC1List() + "|");
//        System.out.println("----citationList:|" + tableData.getCitationList() + "|");
//        System.out.println("----total:|" + tableData.getTotal() + "|");
//        System.out.println("----referenceList:|" + tableData.getReferenceList() + "|");
//        System.out.println("----kwPlusList:|" + tableData.getKwPlusList() + "|");


}
